{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "57263e5a-1cdd-4aff-937e-906510cd6d8f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "b3a31643-3f09-41ad-94c0-4489293fc6d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 用ndarray存储数据\n",
    "score = np.array([[80, 89, 86, 67, 79],\n",
    "[78, 97, 89, 67, 81],\n",
    "[90, 94, 78, 67, 74],\n",
    "[91, 91, 90, 67, 69],\n",
    "[76, 87, 75, 67, 86],\n",
    "[70, 79, 84, 67, 84],\n",
    "[94, 92, 93, 67, 64],\n",
    "[86, 85, 83, 67, 80]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "d461745b-8281-48ac-90d8-c5f51b6686c5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[80, 89, 86, 67, 79],\n",
       "       [78, 97, 89, 67, 81],\n",
       "       [90, 94, 78, 67, 74],\n",
       "       [91, 91, 90, 67, 69],\n",
       "       [76, 87, 75, 67, 86],\n",
       "       [70, 79, 84, 67, 84],\n",
       "       [94, 92, 93, 67, 64],\n",
       "       [86, 85, 83, 67, 80]])"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "id": "ced54793-7dc8-4dc8-802f-14b80fe62f01",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 145,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(score)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35896cee-618d-4516-94d1-528255d936d4",
   "metadata": {},
   "source": [
    "### 3.1.3 ndarray与Python原生list运算效率对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 151,
   "id": "5ba9ac58-383e-4866-832a-302ace3cdbf0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "import time\n",
    "\n",
    "# 生成一个大数组\n",
    "python_list = []\n",
    "\n",
    "for i in range(100000000):\n",
    "    python_list.append(random.random())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "id": "b3f20f9b-a039-4a08-bb02-11dd68ca377f",
   "metadata": {},
   "outputs": [],
   "source": [
    "ndarray_list = np.array(python_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "id": "9dc1fa9f-f3b5-4623-900b-f5d7afea9b7f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "100000000"
      ]
     },
     "execution_count": 150,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(ndarray_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "828410cb-3b1c-4282-8c6f-30ba107ab749",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 原生Python list求和\n",
    "t1 = time.time()\n",
    "a = sum(python_list)\n",
    "t2 = time.time()\n",
    "d1 = t2 - t1\n",
    "\n",
    "# ndarray求和\n",
    "t3 = time.time()\n",
    "b = np.sum(ndarray_list)\n",
    "t4 = time.time()\n",
    "d2 = t4 - t3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "579aec23-2d38-401e-9ecb-8dc464fc8b51",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5111489295959473"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "9c99a85d-f728-4c8a-ab51-d400270e5541",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.14346599578857422"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "9d67c7e8-3d90-4a7d-94ee-1b241106ad89",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "50000453.98424112"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "247d38d8-e7ff-434f-acbb-ed2b8b0818c6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(50000453.98424128)"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "06ca1e2b-e11d-4045-94c1-5a6e37f6d70d",
   "metadata": {},
   "source": [
    "### 3.2.1 ndarray的属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "6a0844c3-237f-46f3-91f4-f28a591e36c2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[80, 89, 86, 67, 79],\n",
       "       [78, 97, 89, 67, 81],\n",
       "       [90, 94, 78, 67, 74],\n",
       "       [91, 91, 90, 67, 69],\n",
       "       [76, 87, 75, 67, 86],\n",
       "       [70, 79, 84, 67, 84],\n",
       "       [94, 92, 93, 67, 64],\n",
       "       [86, 85, 83, 67, 80]])"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "1da0f66f-3e78-499c-8bf2-f16990c5b3ae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(8, 5)"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 形状，二维数据，8行5列的数组(8, 5)\n",
    "score.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "93642fab-a7af-4a35-9b64-16cc5e8bea75",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数组的维度，2维\n",
    "score.ndim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "45766b20-4158-4d47-bac3-a0e5f30ef42c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "40"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数组的大小/元素个数\n",
    "score.size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "494ab4d7-3884-418d-80e8-6148386290d8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int64')"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数组item的类型，如果没有明确指定ndarray的类型，整数型数据的默认类型为int64\n",
    "score.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "8c5e6bae-d664-4c03-b6f4-b8cef02c3df9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数组中一个元素所占的内存大小（单位：字节），一个int64占8个字节\n",
    "score.itemsize"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e2dccd68-3dba-43f0-b67b-926722e1e72e",
   "metadata": {},
   "source": [
    "### 3.2.2 ndarray的形状"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "e9ae41c3-d7ab-4181-9bba-eee1870d29ca",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([[1,2,3],[4,5,6]])\n",
    "b = np.array([1,2,3,4])\n",
    "c = np.array([[[1,2,3],[4,5,6]],[[1,2,3],[4,5,6]]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "4938ab8a-6d1d-488c-9939-e27a87fdb286",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "fd045790-ccfd-4e59-a79d-2d3ed38604d7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4])"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "d295ab18-88d9-4d71-9e3b-0b42e143b339",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[1, 2, 3],\n",
       "        [4, 5, 6]],\n",
       "\n",
       "       [[1, 2, 3],\n",
       "        [4, 5, 6]]])"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "5f477748-ccbd-4be5-8ae4-a9a1183cf3b2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 3)"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# a的形状，二维数据，使用元组表示就是有2个数字，2行3列，(2, 3)\n",
    "a.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "2ec54149-0276-4dbe-b5ef-a2d697237fbc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4,)"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# b的形状，一维数据，使用元组表示就是有1个数字，但是考虑到元组的要求，所以有一个逗号(4, )\n",
    "b.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "39f112a6-631c-4292-9b4f-9dcf7ef7b2f1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 2, 3)"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# c的形状，三维数据，用元组表示就是3个数字，(2, 2, 3)\n",
    "c.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "acbc7eca-e76f-4f24-b958-0fed25ef960c",
   "metadata": {},
   "source": [
    "### 3.2.3 ndarray的类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "af028e98-4d9d-4cca-bcb0-1dc5030a49c6",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = np.array([1.1, 2.2, 3.3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "90928136-259e-4d47-bb37-ea029c201794",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.1, 2.2, 3.3])"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "bb704b9c-724e-4264-9bfc-a4c77b3be62c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('float64')"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 在创建ndarray的时候，如果没有指定类型，小数型数据的默认类型为float64\n",
    "data.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "c9f5be90-b055-43b0-b0b1-34a4d3d21a97",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 在创建ndarray变量的时候手动指定类型\n",
    "a = np.array([1.1, 2.2, 3.3], dtype=np.float32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "70daf91f-dc34-42d9-8f94-f52a4764f443",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('float32')"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "e1647914-111f-44f4-88b3-e87611c556e8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 在创建ndarray变量的时候手动指定类型\n",
    "b = np.array([1,2,3], dtype=\"int32\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "21bb697a-c9b8-42bf-bfd6-d204ae8512f7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int32')"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.dtype"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "41e924e4-4db0-4c42-9d75-30d30fccdc35",
   "metadata": {},
   "source": [
    "### 3.3.1 生成数组的方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c138a958-f417-47c2-8bd7-952d4ab185c9",
   "metadata": {},
   "source": [
    "#### 1. 生成0或1的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "28eb8341-d1dd-4d6c-aec8-b04ce4e27347",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0.]], dtype=float32)"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 生成二维数据，3行4列的0数组\n",
    "np.zeros(shape=(3, 4), dtype=\"float32\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "5c4957e8-958b-48e3-84b1-9bc7c199c081",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 1],\n",
       "       [1, 1, 1]], dtype=int32)"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 生成二维数组，2行3列的1数组\n",
    "np.ones(shape=[2,3], dtype=np.int32)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3891ce4f-f3b2-4be8-a9fe-ee431aa83f66",
   "metadata": {},
   "source": [
    "#### 2. 从现有数组中生成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "db1a2754-291e-4b49-be27-dbe28faeb7b3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[80, 89, 86, 67, 79],\n",
       "       [78, 97, 89, 67, 81],\n",
       "       [90, 94, 78, 67, 74],\n",
       "       [91, 91, 90, 67, 69],\n",
       "       [76, 87, 75, 67, 86],\n",
       "       [70, 79, 84, 67, 84],\n",
       "       [94, 92, 93, 67, 64],\n",
       "       [86, 85, 83, 67, 80]])"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "cd40cf15-a644-4ee4-9ae2-3a821dd25048",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 方法一：np.array()\n",
    "data1 = np.array(score)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "3f3ec3a0-d71f-4ce8-a9f0-26908ff9dcd7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[80, 89, 86, 67, 79],\n",
       "       [78, 97, 89, 67, 81],\n",
       "       [90, 94, 78, 67, 74],\n",
       "       [91, 91, 90, 67, 69],\n",
       "       [76, 87, 75, 67, 86],\n",
       "       [70, 79, 84, 67, 84],\n",
       "       [94, 92, 93, 67, 64],\n",
       "       [86, 85, 83, 67, 80]])"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "1a5ae5a6-e71a-420d-b020-cd269a182112",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 方法二：np.asarray()\n",
    "data2 = np.asarray(score)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "ef5030cf-ec85-4631-b752-813a73bc82e7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[80, 89, 86, 67, 79],\n",
       "       [78, 97, 89, 67, 81],\n",
       "       [90, 94, 78, 67, 74],\n",
       "       [91, 91, 90, 67, 69],\n",
       "       [76, 87, 75, 67, 86],\n",
       "       [70, 79, 84, 67, 84],\n",
       "       [94, 92, 93, 67, 64],\n",
       "       [86, 85, 83, 67, 80]])"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "id": "a9d77aab-f75f-4b5a-8ada-f373e3494dc9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 方法三：np.copy()\n",
    "data3 = np.copy(score)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "c297b03c-e0c7-44b2-b980-61b3cfba1406",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[80, 89, 86, 67, 79],\n",
       "       [78, 97, 89, 67, 81],\n",
       "       [90, 94, 78, 67, 74],\n",
       "       [91, 91, 90, 67, 69],\n",
       "       [76, 87, 75, 67, 86],\n",
       "       [70, 79, 84, 67, 84],\n",
       "       [94, 92, 93, 67, 64],\n",
       "       [86, 85, 83, 67, 80]])"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "id": "fe44a198-84e6-4e56-a62a-1bd98d627508",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 修改源数组的第4行第2列的数据\n",
    "score[3][1] = 10000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "id": "acf8b2f5-7abd-470d-8c15-34eedc7dfc54",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[   80,    89,    86,    67,    79],\n",
       "       [   78,    97,    89,    67,    81],\n",
       "       [   90,    94,    78,    67,    74],\n",
       "       [   91, 10000,    90,    67,    69],\n",
       "       [   76,    87,    75,    67,    86],\n",
       "       [   70,    79,    84,    67,    84],\n",
       "       [   94,    92,    93,    67,    64],\n",
       "       [   86,    85,    83,    67,    80]])"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "9df7b71d-f484-40a2-b207-7ce442dd1e3d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[80, 89, 86, 67, 79],\n",
       "       [78, 97, 89, 67, 81],\n",
       "       [90, 94, 78, 67, 74],\n",
       "       [91, 91, 90, 67, 69],\n",
       "       [76, 87, 75, 67, 86],\n",
       "       [70, 79, 84, 67, 84],\n",
       "       [94, 92, 93, 67, 64],\n",
       "       [86, 85, 83, 67, 80]])"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用np.array()生成的ndarray，不会受源数组变化的影响（值拷贝，深拷贝）\n",
    "data1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "id": "312796a7-4d2c-4b66-8192-fcf753c1f18d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[   80,    89,    86,    67,    79],\n",
       "       [   78,    97,    89,    67,    81],\n",
       "       [   90,    94,    78,    67,    74],\n",
       "       [   91, 10000,    90,    67,    69],\n",
       "       [   76,    87,    75,    67,    86],\n",
       "       [   70,    79,    84,    67,    84],\n",
       "       [   94,    92,    93,    67,    64],\n",
       "       [   86,    85,    83,    67,    80]])"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用np.asarray()生成的ndarray，会受源数组变化的影响（地址拷贝，浅拷贝）\n",
    "data2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "id": "854fdb6b-a75b-45db-ab07-15e807da4217",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[80, 89, 86, 67, 79],\n",
       "       [78, 97, 89, 67, 81],\n",
       "       [90, 94, 78, 67, 74],\n",
       "       [91, 91, 90, 67, 69],\n",
       "       [76, 87, 75, 67, 86],\n",
       "       [70, 79, 84, 67, 84],\n",
       "       [94, 92, 93, 67, 64],\n",
       "       [86, 85, 83, 67, 80]])"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用np.copy()生成的ndarray，不会受源数组变化的影响（值拷贝，深拷贝）\n",
    "data3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8d67fba5-a799-4e81-9efd-7934c8c2f6a1",
   "metadata": {},
   "source": [
    "#### 3. 生成固定范围的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "id": "5592921b-12e5-482d-83a8-8264daa4be9e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0. ,  2.5,  5. ,  7.5, 10. ])"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 在[0, 10]范围内，等距生成5个数的数组\n",
    "np.linspace(0, 10, 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "id": "2bd11a11-921b-4220-85b5-5c23be662940",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 2, 4, 6, 8])"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 在[0, 10)范围内，按步长2生成数组\n",
    "np.arange(0 ,10, 2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3815ffcc-6fdd-4061-ac3c-3f5e8f23a95c",
   "metadata": {},
   "source": [
    "#### 4. 生成随机数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "id": "2d468d87-386f-49d6-985d-ebd91431790d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 生成均匀分布的数组（从-1到1之间，生成1亿个数）\n",
    "data1 = np.random.uniform(low=-1, high=1, size=100000000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "id": "67b718fb-0463-43e4-9fbf-2a5ffdc7b149",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.27043862,  0.03344306,  0.30691215, ..., -0.42019416,\n",
       "        0.84485857, -0.2615655 ], shape=(100000000,))"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "id": "aac8ac32-18c2-4bc3-8dcb-25ab7ccca889",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAABRkAAAIOCAYAAAA88gorAAAAOnRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjEwLjMsIGh0dHBzOi8vbWF0cGxvdGxpYi5vcmcvZiW1igAAAAlwSFlzAAAMTgAADE4Bf3eMIwAAJXRJREFUeJzt3Q+s1XX9P/D31YuY4Z+JIdgV8c9Fp6CI4hCXaM2Nmf+S0DIop4bUzBptyVarrTnSZfRHa6Qr5mIzUcxaaM0sQVc6WZJJhYAiXIJQ1Ol1ahDnt8/H7z1Dfvee+znndc79dx6P7cA9n/fnz/t8zvvz/nzO83w+59NSKpVKCQAAAACgRvvVOiEAAAAAQEbICAAAAACECBkBAAAAgBAhIwAAAAAQImQEAAAAAEKEjAAAAABASGtqEsOHD08f+tCH+rsaAAAAADAovfzyy+ndd99t7pAxCxg7Ojr6uxoAAAAAMCi1tbX1WOZyaQAAAAAgRMgIAAAAAIQIGQEAAACAECEjAAAAABAiZAQAAAAAQoSMA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82c/vtt+ftoatdfOtb38rv8tpFP9Nciu6bsvGyz99vvPHG+6bXzzSXuXPn5hnM/vvvXxo1alTp+OOPz4c7lvn/tWT/dAWOAAAAAADVcrk0AAAAABAiZAQAAAAAQoSMAAAAAECIkBEAAAAACBEyAgAAAAAhQkYAAAAAIETICAAAAACECBkBAAAAgBTx/wCz0Dfr6uPl9QAAAABJRU5ErkJggg==",
      "text/plain": [
       "<Figure size 1600x640 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 通过直方图观察均匀分布结果\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 创建画布\n",
    "plt.figure(figsize=(20, 8), dpi=80)\n",
    "# 绘制直方图\n",
    "plt.hist(data1, 1000)\n",
    "# 显示图像\n",
    "plt.show()\n",
    "# 均匀分布的特点：落在每一点的可能性相等"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "id": "d7ebcb9b-36f4-46fb-8f64-8e241fb5d2f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 生成正泰分布的数组（数据集平均值（对应于分布的中心），标准差（对应于分布的宽度，标准差越大图像越矮胖，标准差越小，图像越瘦高），数据个数）\n",
    "data2 = np.random.normal(loc=1.75, scale=0.1, size=10000000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "id": "ace9802c-7fe3-4c9c-9155-e09f24698565",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.76915447, 1.66545185, 1.63941071, ..., 1.88938781, 1.88860972,\n",
       "       1.77802218], shape=(10000000,))"
      ]
     },
     "execution_count": 123,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "id": "9e7d5d0d-eaf4-4650-8f42-693d3d97d198",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAABRIAAAIOCAYAAADEEvHcAAA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v/AwYJOleaHWVqAAAAAElFTkSuQmCC",
      "text/plain": [
       "<Figure size 1600x640 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 通过直方图观察正态分布结果\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 创建画布\n",
    "plt.figure(figsize=(20, 8), dpi=80)\n",
    "# 绘制直方图\n",
    "plt.hist(data2, 1000)\n",
    "# 显示图像\n",
    "plt.show()\n",
    "# 正态分布的特点：以平均值为轴，左右两侧对称分布"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f742779-003a-40ac-8a25-44e5ed7a3a87",
   "metadata": {},
   "source": [
    "### 案例：随机生成8只股票2周的交易日涨跌幅数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "id": "e363e131-70ff-4583-b8d4-c88cd97a1786",
   "metadata": {},
   "outputs": [],
   "source": [
    "stock_change = np.random.normal(loc=0, scale=1, size=(8, 10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "id": "0844651f-642f-47c4-b2c8-8e5a26eb7146",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.38340172,  0.13973593,  0.62065285, -3.39229407, -0.63984062,\n",
       "        -0.09682208, -0.50699696,  0.58248677,  0.58315606,  0.59025074],\n",
       "       [ 1.99710767,  1.11892802, -0.54543653,  0.42433087,  0.69170305,\n",
       "         1.98378084, -0.19124145, -0.07345897, -0.43643275,  0.02354508],\n",
       "       [ 1.46755235,  1.0342408 , -0.13811798, -0.16970498,  0.52778766,\n",
       "         1.34528741,  1.71738852, -0.43122133, -0.17174528, -1.10143158],\n",
       "       [ 0.47075456,  0.00704928,  2.88722945,  0.39148935,  1.05913149,\n",
       "         1.31434221, -1.39194966, -0.27204515, -0.70690597, -1.82550627],\n",
       "       [-0.39997758, -1.3207629 , -2.06036881, -0.04480483,  0.56875232,\n",
       "        -0.55308282,  1.60448889,  0.6726401 , -1.0633198 ,  1.49211896],\n",
       "       [-0.39692761,  1.02682593,  0.60943873,  1.32321195,  1.59959496,\n",
       "        -0.97076716,  0.62500109,  0.612566  ,  0.69788491,  1.4184571 ],\n",
       "       [ 1.03631795,  0.79025275,  0.83007222,  0.17570968, -0.34306646,\n",
       "         1.7638168 , -0.25557737, -1.34807468,  0.37607408,  1.21622923],\n",
       "       [ 0.82431592, -2.29615911,  0.37121179,  3.34778201, -0.01890529,\n",
       "        -1.8050517 , -1.64594298,  0.04812433,  2.06101825, -0.62104949]])"
      ]
     },
     "execution_count": 126,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_change"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "id": "44044f53-34fd-4027-ab84-f2d962130325",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.38340172, 0.13973593, 0.62065285])"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取第一只股票的前3个交易日的涨跌幅数据\n",
    "stock_change[0, :3:]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "88bed10c-cc51-4a51-ab99-3034dbea1404",
   "metadata": {},
   "source": [
    "### 3.3.2 数据的索引、切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "id": "a1ce6fce-483f-4def-8d75-2a2ca22a187e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 1,  2,  3],\n",
       "        [ 4,  5,  6]],\n",
       "\n",
       "       [[12,  3, 34],\n",
       "        [ 5,  6,  7]]])"
      ]
     },
     "execution_count": 128,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建一个三维数组，并获取其中的值\n",
    "a1 = np.array([[[1,2,3],[4,5,6]], [[12,3, 34], [5,6,7]]])\n",
    "a1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "id": "4db4ee8f-44af-442a-98a1-99c249f12127",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 2, 3)"
      ]
     },
     "execution_count": 129,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数组a1的形状\n",
    "a1.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "id": "d2a8330f-2092-49fc-87eb-68e909e95389",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.int64(34)"
      ]
     },
     "execution_count": 130,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查找（2,1,3）位置的数据\n",
    "a1[1, 0, 2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "id": "1932a236-bc68-4a73-9883-8142c2937b03",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[    1,     2,     3],\n",
       "        [    4,     5,     6]],\n",
       "\n",
       "       [[   12,     3, 10000],\n",
       "        [    5,     6,     7]]])"
      ]
     },
     "execution_count": 131,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 修改（2,1，3）的数据\n",
    "a1[1, 0, 2] = 10000\n",
    "a1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d2da4590-2876-4828-9cec-a156f16d6db7",
   "metadata": {},
   "source": [
    "### 3.3.3 形状修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "id": "e494f401-c513-4837-811d-4f1a8a87b98a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.38340172,  0.13973593,  0.62065285, -3.39229407, -0.63984062,\n",
       "        -0.09682208, -0.50699696,  0.58248677,  0.58315606,  0.59025074],\n",
       "       [ 1.99710767,  1.11892802, -0.54543653,  0.42433087,  0.69170305,\n",
       "         1.98378084, -0.19124145, -0.07345897, -0.43643275,  0.02354508],\n",
       "       [ 1.46755235,  1.0342408 , -0.13811798, -0.16970498,  0.52778766,\n",
       "         1.34528741,  1.71738852, -0.43122133, -0.17174528, -1.10143158],\n",
       "       [ 0.47075456,  0.00704928,  2.88722945,  0.39148935,  1.05913149,\n",
       "         1.31434221, -1.39194966, -0.27204515, -0.70690597, -1.82550627],\n",
       "       [-0.39997758, -1.3207629 , -2.06036881, -0.04480483,  0.56875232,\n",
       "        -0.55308282,  1.60448889,  0.6726401 , -1.0633198 ,  1.49211896],\n",
       "       [-0.39692761,  1.02682593,  0.60943873,  1.32321195,  1.59959496,\n",
       "        -0.97076716,  0.62500109,  0.612566  ,  0.69788491,  1.4184571 ],\n",
       "       [ 1.03631795,  0.79025275,  0.83007222,  0.17570968, -0.34306646,\n",
       "         1.7638168 , -0.25557737, -1.34807468,  0.37607408,  1.21622923],\n",
       "       [ 0.82431592, -2.29615911,  0.37121179,  3.34778201, -0.01890529,\n",
       "        -1.8050517 , -1.64594298,  0.04812433,  2.06101825, -0.62104949]])"
      ]
     },
     "execution_count": 132,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 需求：让刚才生成的股票行、日期列反过来，变成日期行，股票列（转置）\n",
    "stock_change"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "id": "42cdb4e8-fcf2-479f-9f16-b0b2a47d2d0f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(8, 10)"
      ]
     },
     "execution_count": 133,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看stock_change的形状\n",
    "stock_change.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "id": "a552c4fd-3811-49ed-af6b-a6a5ebf23592",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.38340172,  0.13973593,  0.62065285, -3.39229407, -0.63984062,\n",
       "        -0.09682208, -0.50699696,  0.58248677],\n",
       "       [ 0.58315606,  0.59025074,  1.99710767,  1.11892802, -0.54543653,\n",
       "         0.42433087,  0.69170305,  1.98378084],\n",
       "       [-0.19124145, -0.07345897, -0.43643275,  0.02354508,  1.46755235,\n",
       "         1.0342408 , -0.13811798, -0.16970498],\n",
       "       [ 0.52778766,  1.34528741,  1.71738852, -0.43122133, -0.17174528,\n",
       "        -1.10143158,  0.47075456,  0.00704928],\n",
       "       [ 2.88722945,  0.39148935,  1.05913149,  1.31434221, -1.39194966,\n",
       "        -0.27204515, -0.70690597, -1.82550627],\n",
       "       [-0.39997758, -1.3207629 , -2.06036881, -0.04480483,  0.56875232,\n",
       "        -0.55308282,  1.60448889,  0.6726401 ],\n",
       "       [-1.0633198 ,  1.49211896, -0.39692761,  1.02682593,  0.60943873,\n",
       "         1.32321195,  1.59959496, -0.97076716],\n",
       "       [ 0.62500109,  0.612566  ,  0.69788491,  1.4184571 ,  1.03631795,\n",
       "         0.79025275,  0.83007222,  0.17570968],\n",
       "       [-0.34306646,  1.7638168 , -0.25557737, -1.34807468,  0.37607408,\n",
       "         1.21622923,  0.82431592, -2.29615911],\n",
       "       [ 0.37121179,  3.34778201, -0.01890529, -1.8050517 , -1.64594298,\n",
       "         0.04812433,  2.06101825, -0.62104949]])"
      ]
     },
     "execution_count": 134,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 调用ndarray.reshape()方法，将二维的8行10列改为二维的10行8列，该方法只是做了数据的分割并返回，并没有让行列互换，原始数据没有改变\n",
    "stock_change.reshape((10, 8))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "id": "58b135b4-b060-4edc-ad35-258c12c8072a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 调用ndarray.resize()方法，将二维的8行10列改为二维的10行8列，该方法也只是做了数据的分割，该方法没有返回值，但是会对原始的ndarray进行了修改，\n",
    "stock_change.resize((10, 8))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "id": "9a712607-24e6-48d9-bf81-c47c642d8f0a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.38340172,  0.13973593,  0.62065285, -3.39229407, -0.63984062,\n",
       "        -0.09682208, -0.50699696,  0.58248677],\n",
       "       [ 0.58315606,  0.59025074,  1.99710767,  1.11892802, -0.54543653,\n",
       "         0.42433087,  0.69170305,  1.98378084],\n",
       "       [-0.19124145, -0.07345897, -0.43643275,  0.02354508,  1.46755235,\n",
       "         1.0342408 , -0.13811798, -0.16970498],\n",
       "       [ 0.52778766,  1.34528741,  1.71738852, -0.43122133, -0.17174528,\n",
       "        -1.10143158,  0.47075456,  0.00704928],\n",
       "       [ 2.88722945,  0.39148935,  1.05913149,  1.31434221, -1.39194966,\n",
       "        -0.27204515, -0.70690597, -1.82550627],\n",
       "       [-0.39997758, -1.3207629 , -2.06036881, -0.04480483,  0.56875232,\n",
       "        -0.55308282,  1.60448889,  0.6726401 ],\n",
       "       [-1.0633198 ,  1.49211896, -0.39692761,  1.02682593,  0.60943873,\n",
       "         1.32321195,  1.59959496, -0.97076716],\n",
       "       [ 0.62500109,  0.612566  ,  0.69788491,  1.4184571 ,  1.03631795,\n",
       "         0.79025275,  0.83007222,  0.17570968],\n",
       "       [-0.34306646,  1.7638168 , -0.25557737, -1.34807468,  0.37607408,\n",
       "         1.21622923,  0.82431592, -2.29615911],\n",
       "       [ 0.37121179,  3.34778201, -0.01890529, -1.8050517 , -1.64594298,\n",
       "         0.04812433,  2.06101825, -0.62104949]])"
      ]
     },
     "execution_count": 136,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_change"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "id": "7395d0a7-1c81-4d63-8617-b6bb65e96eda",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 将stock_change改回成8行10列的二维数据\n",
    "stock_change.resize((8, 10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "id": "8f3d1cb4-563c-442b-89f8-f47bd88b71a9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.38340172,  0.13973593,  0.62065285, -3.39229407, -0.63984062,\n",
       "        -0.09682208, -0.50699696,  0.58248677,  0.58315606,  0.59025074],\n",
       "       [ 1.99710767,  1.11892802, -0.54543653,  0.42433087,  0.69170305,\n",
       "         1.98378084, -0.19124145, -0.07345897, -0.43643275,  0.02354508],\n",
       "       [ 1.46755235,  1.0342408 , -0.13811798, -0.16970498,  0.52778766,\n",
       "         1.34528741,  1.71738852, -0.43122133, -0.17174528, -1.10143158],\n",
       "       [ 0.47075456,  0.00704928,  2.88722945,  0.39148935,  1.05913149,\n",
       "         1.31434221, -1.39194966, -0.27204515, -0.70690597, -1.82550627],\n",
       "       [-0.39997758, -1.3207629 , -2.06036881, -0.04480483,  0.56875232,\n",
       "        -0.55308282,  1.60448889,  0.6726401 , -1.0633198 ,  1.49211896],\n",
       "       [-0.39692761,  1.02682593,  0.60943873,  1.32321195,  1.59959496,\n",
       "        -0.97076716,  0.62500109,  0.612566  ,  0.69788491,  1.4184571 ],\n",
       "       [ 1.03631795,  0.79025275,  0.83007222,  0.17570968, -0.34306646,\n",
       "         1.7638168 , -0.25557737, -1.34807468,  0.37607408,  1.21622923],\n",
       "       [ 0.82431592, -2.29615911,  0.37121179,  3.34778201, -0.01890529,\n",
       "        -1.8050517 , -1.64594298,  0.04812433,  2.06101825, -0.62104949]])"
      ]
     },
     "execution_count": 138,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_change"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "id": "19047235-87cf-4633-baa5-4dc6efd9c685",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.38340172,  1.99710767,  1.46755235,  0.47075456, -0.39997758,\n",
       "        -0.39692761,  1.03631795,  0.82431592],\n",
       "       [ 0.13973593,  1.11892802,  1.0342408 ,  0.00704928, -1.3207629 ,\n",
       "         1.02682593,  0.79025275, -2.29615911],\n",
       "       [ 0.62065285, -0.54543653, -0.13811798,  2.88722945, -2.06036881,\n",
       "         0.60943873,  0.83007222,  0.37121179],\n",
       "       [-3.39229407,  0.42433087, -0.16970498,  0.39148935, -0.04480483,\n",
       "         1.32321195,  0.17570968,  3.34778201],\n",
       "       [-0.63984062,  0.69170305,  0.52778766,  1.05913149,  0.56875232,\n",
       "         1.59959496, -0.34306646, -0.01890529],\n",
       "       [-0.09682208,  1.98378084,  1.34528741,  1.31434221, -0.55308282,\n",
       "        -0.97076716,  1.7638168 , -1.8050517 ],\n",
       "       [-0.50699696, -0.19124145,  1.71738852, -1.39194966,  1.60448889,\n",
       "         0.62500109, -0.25557737, -1.64594298],\n",
       "       [ 0.58248677, -0.07345897, -0.43122133, -0.27204515,  0.6726401 ,\n",
       "         0.612566  , -1.34807468,  0.04812433],\n",
       "       [ 0.58315606, -0.43643275, -0.17174528, -0.70690597, -1.0633198 ,\n",
       "         0.69788491,  0.37607408,  2.06101825],\n",
       "       [ 0.59025074,  0.02354508, -1.10143158, -1.82550627,  1.49211896,\n",
       "         1.4184571 ,  1.21622923, -0.62104949]])"
      ]
     },
     "execution_count": 139,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 调用ndarray.T属性，实现数组的转置（行列真的互换）\n",
    "stock_change.T"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "26c25e13-febe-46e5-bd18-0f0556735784",
   "metadata": {},
   "source": [
    "### 3.3.4 类型修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 152,
   "id": "5de268f8-ee79-434e-84d0-c4a4f3a36637",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 152,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(stock_change)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "id": "229e4615-52a7-4b2a-ad91-3552cdc1b3e2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  0,  0, -3,  0,  0,  0,  0,  0,  0],\n",
       "       [ 1,  1,  0,  0,  0,  1,  0,  0,  0,  0],\n",
       "       [ 1,  1,  0,  0,  0,  1,  1,  0,  0, -1],\n",
       "       [ 0,  0,  2,  0,  1,  1, -1,  0,  0, -1],\n",
       "       [ 0, -1, -2,  0,  0,  0,  1,  0, -1,  1],\n",
       "       [ 0,  1,  0,  1,  1,  0,  0,  0,  0,  1],\n",
       "       [ 1,  0,  0,  0,  0,  1,  0, -1,  0,  1],\n",
       "       [ 0, -2,  0,  3,  0, -1, -1,  0,  2,  0]], dtype=int32)"
      ]
     },
     "execution_count": 153,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 修改类型\n",
    "stock_change.astype(\"int32\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 154,
   "id": "6ca25b56-2afc-4be0-9547-a9922bb185db",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 154,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(stock_change)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 156,
   "id": "99036435-2982-4d7e-bb36-6fb7a3c985d4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'-X\\xbf\\xd8i\"\\xf6?\\x99\\xd4\\x89\\xf1\\xdd\\xe2\\xc1?L6\\xce[c\\xdc\\xe3?\\xa3]&\\x12k#\\x0b\\xc0\\xf6\\xdc\\xf8\\x08\\x93y\\xe4\\xbf\\x83Z\\x16\\x05U\\xc9\\xb8\\xbf\\xc1\\xb7\\xf7\\xb0Q9\\xe0\\xbf\\x08\\x8a\\xfeH\\xbb\\xa3\\xe2?\\xff\\xa1\\x9b\\xe76\\xa9\\xe2?d\\x9aI\\x83U\\xe3\\xe2?\\xbc\\xdeK-\\'\\xf4\\xff?\\x86\\xca\\xb4\\x11!\\xe7\\xf1?\\x1c~\\x9eP7t\\xe1\\xbf\\x83Ja\\xab<(\\xdb?\\x01)\\xbann\"\\xe6?\\x8e\\x9a\\x98\\xfb\\x90\\xbd\\xff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xf7TpK\\xe0+\\xf5?Xt\\xb9\\xe2\\xf0\\x97\\xf9?B\\x85KK\\x86\\x10\\xef\\xbf\\xfdEoG\\x02\\x00\\xe4?\\xf7\\x87\\xba\\x05$\\x9a\\xe3?/\\x8f\\xc0\\xbe\\x12U\\xe6?\\x86)\\xcf\\x11\\x00\\xb2\\xf6?S\\xb0)!\\xc2\\x94\\xf0?\\t\\x11\\xce\"\\xc0I\\xe9?\\xf3a\\xff\\x9f\\xf3\\x8f\\xea?j\\xb62\\xa1\\xa7}\\xc6?\\x0bt@\\x03\\xcd\\xf4\\xd5\\xbf\\x9a\\x9fQ\\xf7\\x978\\xfc?\\xa2\\x8e\\xd40a[\\xd0\\xbf\\x12I\\xbb\\xc1\\xb6\\x91\\xf5\\xbf\\xa2W\\xb6\\x06\\x99\\x11\\xd8?|\\xfa\\xf2\\xc8\\xacu\\xf3?k\\x198\\xc8\\xcb`\\xea?\\xf2W.\\xab\\x88^\\x02\\xc0\\x06\\x98\\x03\\x1d\\xef\\xc1\\xd7?R\\xab\\xdd\\xeeA\\xc8\\n@\\xa4\\x8dn\\xca\\xe8[\\x93\\xbf\\xbce\\xa6\\xe3}\\xe1\\xfc\\xbf\\x83\\x98\\xa2M\\xc8U\\xfa\\xbf\\x91\\x03\\x1cs\\xc0\\xa3\\xa8?!d{#\\xf7|\\x00@\\xf4S\\xd4+\\xa3\\xdf\\xe3\\xbf'"
      ]
     },
     "execution_count": 156,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 序列化\n",
    "stock_change.tobytes()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "726f353d-085e-464f-a83d-1a0740b4f665",
   "metadata": {},
   "source": [
    "### 3.3.5 数组的去重"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 157,
   "id": "724138ba-e771-4bec-b271-cd6de4021c15",
   "metadata": {},
   "outputs": [],
   "source": [
    "temp = np.array([[1,2,3,4],[3,4,5,6]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 158,
   "id": "5052ebdf-b515-4166-9ea6-29de2818f6c1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3, 4],\n",
       "       [3, 4, 5, 6]])"
      ]
     },
     "execution_count": 158,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "temp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 160,
   "id": "64ca5f6d-7675-4320-a710-58ca8a16c2b7",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "unhashable type: 'numpy.ndarray'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[160], line 2\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;66;03m# 尝试用python原生的set()方法去重，发现会报错\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m \u001b[38;5;28;43mset\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mtemp\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[1;31mTypeError\u001b[0m: unhashable type: 'numpy.ndarray'"
     ]
    }
   ],
   "source": [
    "# 尝试用python原生的set()方法去重，发现会报错\n",
    "set(temp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 165,
   "id": "3eca71a6-de92-4aa7-aced-526a2a9772b5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5, 6])"
      ]
     },
     "execution_count": 165,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用ndarray.unique()方法实现数组去重，即可实现去重\n",
    "np.unique(temp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 166,
   "id": "8c3a2e8e-6d8e-4106-88ac-2a5d366f0858",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{np.int64(1), np.int64(2), np.int64(3), np.int64(4), np.int64(5), np.int64(6)}"
      ]
     },
     "execution_count": 166,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 如果就是想用set()方法去重，需要先将二维数组 转换成一维数组，然后再去重\n",
    "set(temp.flatten())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "87d00988-f551-45f5-90c2-4199607f5ef2",
   "metadata": {},
   "source": [
    "## 3.4 ndarray的运算"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c5dd9ebb-5a9b-477a-8be1-c6c37542cd9e",
   "metadata": {},
   "source": [
    "### 3.4.1 逻辑运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "cda8b312-42da-487b-9751-e49b24b3ce2f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 重新随机生成8只股票2周的交易日涨跌幅数据\n",
    "stock_change = np.random.normal(loc=0, scale=1, size=(8, 10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c2bf033a-e3d5-4fec-9ed7-61d2a2f09882",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.70291307,  0.53195633,  0.18056258, -0.32740099, -0.491918  ,\n",
       "         0.28672013,  0.47454644,  0.37638284, -0.46812216, -1.14023284],\n",
       "       [ 2.27595304,  0.55068195,  1.02376193, -1.65849329, -1.52797324,\n",
       "         1.33564307,  0.34201156,  0.96689225, -0.20882669,  2.00157069],\n",
       "       [ 0.86916224, -0.00606215,  1.29142681, -0.97833641, -1.35458347,\n",
       "         0.04113526,  0.12275939,  0.41993826, -0.1811256 , -0.12220553],\n",
       "       [-0.88159053, -0.85980743,  0.78020155, -1.01473223, -0.44282743,\n",
       "         0.06094188,  0.67434926,  2.47678192,  0.01317977, -0.42182721],\n",
       "       [-1.1030224 ,  1.27678673, -2.26483171,  0.03692056,  0.88126238,\n",
       "        -0.03459643,  1.27342174,  0.03390794,  0.5940841 , -0.09502462],\n",
       "       [ 0.50110529, -0.18500193,  1.27974572,  2.17930702,  0.57940493,\n",
       "        -0.77903953,  0.09140619,  0.57022863,  0.2966503 , -1.00834934],\n",
       "       [-0.38379117, -0.21843981, -0.26080385, -0.75733493,  0.87036532,\n",
       "         1.05251766,  0.08279958, -1.17450256,  0.15447672, -0.0802633 ],\n",
       "       [-0.80474212,  1.51457707,  1.08560847, -0.48773072,  0.0159016 ,\n",
       "         0.06445782, -0.16155624, -0.73477694,  0.02829189,  0.10497304]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_change"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e8684eb1-8018-45ae-a928-de424ba5bac5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ True,  True, False, False, False, False, False, False, False,\n",
       "         True],\n",
       "       [ True,  True,  True,  True,  True,  True, False,  True, False,\n",
       "         True],\n",
       "       [ True, False,  True,  True,  True, False, False, False, False,\n",
       "        False],\n",
       "       [ True,  True,  True,  True, False, False,  True,  True, False,\n",
       "        False],\n",
       "       [ True,  True,  True, False,  True, False,  True, False,  True,\n",
       "        False],\n",
       "       [ True, False,  True,  True,  True,  True, False,  True, False,\n",
       "         True],\n",
       "       [False, False, False,  True,  True,  True, False,  True, False,\n",
       "        False],\n",
       "       [ True,  True,  True, False, False, False, False,  True, False,\n",
       "        False]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 逻辑判断，如果涨跌幅大于0.5就标记为True，否则为Flase\n",
    "(stock_change > 0.5) | (stock_change < -0.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "55c51270-ebb0-48e8-afb8-4cad0ee0f108",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 通过布尔索引，将所有涨幅超过0.5的数据改为1.1\n",
    "stock_change[stock_change > 0.5] = 1.1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e84f6d2f-cb1e-4eee-a710-7fb83e806ada",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.1       ,  1.1       ,  0.18056258, -0.32740099, -0.491918  ,\n",
       "         0.28672013,  0.47454644,  0.37638284, -0.46812216, -1.14023284],\n",
       "       [ 1.1       ,  1.1       ,  1.1       , -1.65849329, -1.52797324,\n",
       "         1.1       ,  0.34201156,  1.1       , -0.20882669,  1.1       ],\n",
       "       [ 1.1       , -0.00606215,  1.1       , -0.97833641, -1.35458347,\n",
       "         0.04113526,  0.12275939,  0.41993826, -0.1811256 , -0.12220553],\n",
       "       [-0.88159053, -0.85980743,  1.1       , -1.01473223, -0.44282743,\n",
       "         0.06094188,  1.1       ,  1.1       ,  0.01317977, -0.42182721],\n",
       "       [-1.1030224 ,  1.1       , -2.26483171,  0.03692056,  1.1       ,\n",
       "        -0.03459643,  1.1       ,  0.03390794,  1.1       , -0.09502462],\n",
       "       [ 1.1       , -0.18500193,  1.1       ,  1.1       ,  1.1       ,\n",
       "        -0.77903953,  0.09140619,  1.1       ,  0.2966503 , -1.00834934],\n",
       "       [-0.38379117, -0.21843981, -0.26080385, -0.75733493,  1.1       ,\n",
       "         1.1       ,  0.08279958, -1.17450256,  0.15447672, -0.0802633 ],\n",
       "       [-0.80474212,  1.1       ,  1.1       , -0.48773072,  0.0159016 ,\n",
       "         0.06445782, -0.16155624, -0.73477694,  0.02829189,  0.10497304]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_change"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de830ea3-41b8-46c4-812d-7a92cbfbda14",
   "metadata": {},
   "source": [
    "### 3.4.2 通用判断函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9e5b0f61-cb5a-43d2-9373-af4ce99c36d3",
   "metadata": {},
   "source": [
    "##### 1. np.all()传入一组布尔值，这组布尔值中只要有一个Flase就返回Flase，只有在全是True的情况下，才返回True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "eb74e272-0ea5-4bf5-8f25-96eceecdb775",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ True,  True,  True, False, False],\n",
       "       [ True,  True,  True, False, False]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 判断stock_change[0:2, 0:5]（前两只股票的前五个交易日）是否全是上涨的\n",
    "stock_change[0:2, 0:5] > 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "23214e87-4414-4075-b9ad-9f9415b775f2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.False_"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.all(stock_change[0:2, 0:5] > 0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cc9fb4cc-8fd8-4270-9b38-6b5dcb40d2fb",
   "metadata": {},
   "source": [
    "#### 2. np.any()传入一组布尔值，这组布尔值中只要有一个True就返回True，只有全是Flase的情况下，才会返回False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "1fc553aa-94ae-4f00-87e2-79aa00d62b69",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ True,  True,  True, False, False,  True,  True,  True, False,\n",
       "        False],\n",
       "       [ True,  True,  True, False, False,  True,  True,  True, False,\n",
       "         True],\n",
       "       [ True, False,  True, False, False,  True,  True,  True, False,\n",
       "        False],\n",
       "       [False, False,  True, False, False,  True,  True,  True,  True,\n",
       "        False],\n",
       "       [False,  True, False,  True,  True, False,  True,  True,  True,\n",
       "        False]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 判断前5只股票是否有上涨的\n",
    "stock_change[:5] > 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "a2a0644a-7922-4a15-b658-24120cb12055",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.True_"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.any(stock_change[:5] > 0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c76b4d47-424b-4412-889f-a37d51a749df",
   "metadata": {},
   "source": [
    "### 3.4.3 np.where三元运算符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "a95b0a21-dc65-4048-bf19-6c384461ccae",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 判断前四只股票前四天的涨跌幅，大于0的置为1，否则置为0\n",
    "temp = stock_change[:4, :4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "12a29d12-070c-4eb9-9e2c-b4b232e99a7c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.1       ,  1.1       ,  0.18056258, -0.32740099],\n",
       "       [ 1.1       ,  1.1       ,  1.1       , -1.65849329],\n",
       "       [ 1.1       , -0.00606215,  1.1       , -0.97833641],\n",
       "       [-0.88159053, -0.85980743,  1.1       , -1.01473223]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "temp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "12aad8bf-d514-4a5b-8d88-5d702a685ac6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 1, 0],\n",
       "       [1, 1, 1, 0],\n",
       "       [1, 0, 1, 0],\n",
       "       [0, 0, 1, 0]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.where(temp > 0, 1, 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "a02abd79-78b1-475d-b59b-f6b88e666483",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[False, False, False, False],\n",
       "       [False, False, False, False],\n",
       "       [False, False, False, False],\n",
       "       [False, False, False, False]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 判断前四个股票前四天的涨跌幅 大于0.5并且小于1的，换为1，否则为0\n",
    "# (temp > 0.5) & (temp < 1)\n",
    "# 与上一行代码效果完全一样\n",
    "np.logical_and(temp > 0.5, temp < 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "2a848e4c-da83-4ea0-9a12-8ba0cfd700b1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0, 0],\n",
       "       [0, 0, 0, 0],\n",
       "       [0, 0, 0, 0],\n",
       "       [0, 0, 0, 0]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.where(np.logical_and(temp > 0.5, temp < 1), 1, 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "7ce28045-7e4b-4099-b4b4-92ada01eb11b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ True,  True, False, False],\n",
       "       [ True,  True,  True,  True],\n",
       "       [ True, False,  True,  True],\n",
       "       [ True,  True,  True,  True]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 判断前四个股票前四天的涨跌幅 大于0.5或者小于-0.5的，换为1，否则为0\n",
    "# (temp > 0.5) | (temp < -0.5)\n",
    "# 与上一行代码效果完全一样\n",
    "np.logical_or(temp > 0.5, temp < -0.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "25ec863c-5f10-45fc-8fce-2e41f9945702",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 0, 0],\n",
       "       [1, 1, 1, 1],\n",
       "       [1, 0, 1, 1],\n",
       "       [1, 1, 1, 1]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.where(np.logical_or(temp > 0.5, temp < -0.5), 1, 0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20d3ce29-dd2d-4bf3-980d-7f21010b3aea",
   "metadata": {},
   "source": [
    "### 3.4.4 统计运算"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a63e63fe-b7cd-41a1-935c-4a84c08ba4a4",
   "metadata": {},
   "source": [
    "#### 2. 股票涨幅统计运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "83e13685-6baf-421d-baaf-37e3d6a40737",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.1       ,  1.1       ,  0.18056258, -0.32740099],\n",
       "       [ 1.1       ,  1.1       ,  1.1       , -1.65849329],\n",
       "       [ 1.1       , -0.00606215,  1.1       , -0.97833641],\n",
       "       [-0.88159053, -0.85980743,  1.1       , -1.01473223]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 前四只股票前四天的最大涨幅\n",
    "temp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "4af3ac65-d1fa-4968-b1de-2264dca5b20d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(1.1)"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "temp.max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "ed96a62f-441a-400a-b723-1dd12c75a40f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(1.1)"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.max(temp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "4948bb56-e076-4a18-9a97-d525e40aabba",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1.1       ,  1.1       ,  1.1       , -0.32740099])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 统计前四只股票每一天的最大涨幅（求每一列的最大值）\n",
    "temp.max(axis=0)\n",
    "np.max(temp, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "39a30059-902f-4030-a464-f887a146d431",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.1, 1.1, 1.1, 1.1])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 统计前四只股票前四天，每只股票的最大涨幅（求每一行的最大值）\n",
    "temp.max(axis=1)\n",
    "np.max(temp, axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3dc6dd40-9680-4a6f-9f57-1e67f2a148bb",
   "metadata": {},
   "source": [
    "- axis的值其实是ndarray数组的维度，如上面的二维数组，填0时是用最外层的数据，而填1时，则是用里层的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "fb1d7b35-2017-452b-a3ce-9f4337348c78",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 1, 0])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 统计前四只股票每一天的最大涨幅（求每一列的最大值）的下标\n",
    "temp.argmax(axis=0)\n",
    "np.argmax(temp, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "39c501b1-d6b1-48a6-b213-194eb9ef5a01",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, 2])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 统计前四只股票前四天，每只股票的最大涨幅（求每一行的最大值）的下标\n",
    "temp.argmax(axis=1)\n",
    "np.argmax(temp, axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6ae78b48-5d68-4448-9bf2-dc344f2cb03c",
   "metadata": {},
   "source": [
    "## 3.5 数组间运算"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8697f7b4-415f-45ec-b1c7-0c6e1a96c480",
   "metadata": {},
   "source": [
    "### 3.5.2 数组与数的运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9473eba7-302f-4108-bad4-adaccb67058f",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr = np.array([[1, 2, 3, 2, 1, 4], [5, 6, 1, 2, 3, 1]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9cc7ebe5-1037-4bdd-80ea-91787ac672bc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3, 2, 1, 4],\n",
       "       [5, 6, 1, 2, 3, 1]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d851652e-3c30-40f9-8108-0ea0b2371e9d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 3, 4, 3, 2, 5],\n",
       "       [6, 7, 2, 3, 4, 2]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 对所有元素都加1\n",
    "arr + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "933385cd-bc4d-45a3-8f72-689b347df269",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.1, 0.2, 0.3, 0.2, 0.1, 0.4],\n",
       "       [0.5, 0.6, 0.1, 0.2, 0.3, 0.1]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 加减乘除都是可以的\n",
    "arr / 10"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6404b4e3-cbc7-469e-9752-4b224a239eb6",
   "metadata": {},
   "source": [
    "### 3.5.3 数组与数组的运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "f0e73531-ecb4-436d-b792-424e04ffb5f1",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr1 = np.array([[1, 2, 3, 2, 1, 4], [5, 6, 1, 2, 3, 1]])\n",
    "arr2 = np.array([[1, 2, 3, 4], [3, 4, 5, 6]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "02a8015a-9720-4734-a78f-9690ad6457a0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3, 2, 1, 4],\n",
       "       [5, 6, 1, 2, 3, 1]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "68be9927-644c-4c6c-aeb1-171f0e6d431c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3, 4],\n",
       "       [3, 4, 5, 6]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "ae9898ad-f772-4485-998f-ee622a228274",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "operands could not be broadcast together with shapes (2,6) (2,4) ",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mValueError\u001b[39m                                Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[15]\u001b[39m\u001b[32m, line 2\u001b[39m\n\u001b[32m      1\u001b[39m \u001b[38;5;66;03m# 形状完全不相同的两个数组是不能运算的\u001b[39;00m\n\u001b[32m----> \u001b[39m\u001b[32m2\u001b[39m \u001b[43marr1\u001b[49m\u001b[43m \u001b[49m\u001b[43m+\u001b[49m\u001b[43m \u001b[49m\u001b[43marr2\u001b[49m\n",
      "\u001b[31mValueError\u001b[39m: operands could not be broadcast together with shapes (2,6) (2,4) "
     ]
    }
   ],
   "source": [
    "# 形状完全不相同的两个数组是不能运算的\n",
    "arr1 + arr2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9a13278d-e0f7-4656-a927-4ff81c749290",
   "metadata": {},
   "source": [
    "> 当操作两个数组时，numpy会逐个比较数组的shape（形状，即构成的元组tuple），只有在下述两种情况下，两个数组才能够进行数组与数组的运算：\n",
    "> - 维度相同\n",
    "> - shape（其中相对应的一个地方为1）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "13ba7e12-f52a-4e98-893b-88bd7864eb0a",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr1 = np.array([[1, 2, 3, 2, 1, 4], [5, 6, 1, 2, 3, 1]])\n",
    "arr2 = np.array([[1], [3]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "a6181c83-d16e-41a0-849d-1774350627c9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 6)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "c5fd0286-7d0b-402f-823e-0425a68ef449",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 1)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "2e9a1e9e-49f5-4ac0-84ae-29840831bc63",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 3, 4, 3, 2, 5],\n",
       "       [8, 9, 4, 5, 6, 4]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 + arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "d63f9f96-e122-4ec5-b811-e28cbaa55aa3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3,  2,  1,  4],\n",
       "       [15, 18,  3,  6,  9,  3]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 * arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "7ad74271-67c4-45ed-be76-e649c35447df",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.        , 2.        , 3.        , 2.        , 1.        ,\n",
       "        4.        ],\n",
       "       [1.66666667, 2.        , 0.33333333, 0.66666667, 1.        ,\n",
       "        0.33333333]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 / arr2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a1b579fd-c862-4729-9ee8-487e4510ff73",
   "metadata": {},
   "source": [
    "### 3.5.5 矩阵运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "b7b604b9-730f-479c-9938-762807d89cb9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 用ndarray存储矩阵\n",
    "data = np.array([[80, 86],\n",
    "[82, 80],\n",
    "[85, 78],\n",
    "[90, 90],\n",
    "[86, 82],\n",
    "[82, 90],\n",
    "[78, 80],\n",
    "[92, 94]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "7f224cd8-5f19-43a4-a953-288059b4bbbe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[80, 86],\n",
       "       [82, 80],\n",
       "       [85, 78],\n",
       "       [90, 90],\n",
       "       [86, 82],\n",
       "       [82, 90],\n",
       "       [78, 80],\n",
       "       [92, 94]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "a85f3fef-7b77-4032-a98f-2668dd9acbfe",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 用matrix存储矩阵\n",
    "data_mat = np.asmatrix([[80, 86],\n",
    "[82, 80],\n",
    "[85, 78],\n",
    "[90, 90],\n",
    "[86, 82],\n",
    "[82, 90],\n",
    "[78, 80],\n",
    "[92, 94]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "58aabb90-e325-46ce-87f7-0aa8cd047757",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[80, 86],\n",
       "        [82, 80],\n",
       "        [85, 78],\n",
       "        [90, 90],\n",
       "        [86, 82],\n",
       "        [82, 90],\n",
       "        [78, 80],\n",
       "        [92, 94]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_mat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "c69ef7da-6f0a-4b46-a9dc-4fd8798d1186",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.matrix"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(data_mat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "34ce3aa1-3161-4464-bb05-2ee60fe6de99",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 假设data中的数据是学生的平时成绩和考试成绩，现在要求所有学生的平时成绩*0.3+考试成绩*0.7得出最终成绩\n",
    "# 生成权重举证（2行，1列）\n",
    "weight = np.array([[0.3], [0.7]])\n",
    "weight_mat = np.asmatrix([[0.3], [0.7]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "c0d0e9b7-ad32-41d9-80fb-d4302236213c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 如果用ndarray存储矩阵，必须使用np.matmul()方法或np.dot()方法实现矩阵相乘\n",
    "score = np.matmul(data, weight)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "38008628-5937-4e33-ac01-8e57c6aca21e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[84.2],\n",
       "       [80.6],\n",
       "       [80.1],\n",
       "       [90. ],\n",
       "       [83.2],\n",
       "       [87.6],\n",
       "       [79.4],\n",
       "       [93.4]])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "ce396c7b-aa67-4df3-9344-e221040dbbf7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 同时，如果是matrix存储矩阵，既可以用p.matmul()方法或np.dot()方法实现矩阵相乘，也可以直接进行matrix相乘\n",
    "score_mat = np.matmul(data_mat, weight_mat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "d3be6173-90ef-4d1d-9b74-9871743b8220",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[84.2],\n",
       "        [80.6],\n",
       "        [80.1],\n",
       "        [90. ],\n",
       "        [83.2],\n",
       "        [87.6],\n",
       "        [79.4],\n",
       "        [93.4]])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "score_mat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "8e20073c-22ee-46eb-a256-d2292ea775d6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[84.2],\n",
       "       [80.6],\n",
       "       [80.1],\n",
       "       [90. ],\n",
       "       [83.2],\n",
       "       [87.6],\n",
       "       [79.4],\n",
       "       [93.4]])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dot(data, weight)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "341fa8c6-9577-4fd3-9c31-bd7512a4150b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[84.2],\n",
       "        [80.6],\n",
       "        [80.1],\n",
       "        [90. ],\n",
       "        [83.2],\n",
       "        [87.6],\n",
       "        [79.4],\n",
       "        [93.4]])"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dot(data_mat, weight_mat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "747ad60c-e87d-44c9-ad86-79d206aa3601",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[84.2],\n",
       "        [80.6],\n",
       "        [80.1],\n",
       "        [90. ],\n",
       "        [83.2],\n",
       "        [87.6],\n",
       "        [79.4],\n",
       "        [93.4]])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 如果使用matrix类型存储矩阵，也可以直接相乘\n",
    "data_mat * weight_mat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "2ca292cb-6d79-4f6a-ba3e-de15dec6d8c6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[84.2],\n",
       "       [80.6],\n",
       "       [80.1],\n",
       "       [90. ],\n",
       "       [83.2],\n",
       "       [87.6],\n",
       "       [79.4],\n",
       "       [93.4]])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 如果用ndarray存储矩阵，想用运算符直接相乘，也是可以的，需要用@运算符代表相乘\n",
    "data @ weight"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "1c87193e-fac7-4516-afa8-a16997476bc4",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "operands could not be broadcast together with shapes (8,2) (2,1) ",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mValueError\u001b[39m                                Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[47]\u001b[39m\u001b[32m, line 2\u001b[39m\n\u001b[32m      1\u001b[39m \u001b[38;5;66;03m# 树组间的运算必须要满足形状的要求（广播机制），即：维度相同 或 shape（其中相对应的一个地方为1），所以不能直接进行运算，会报错\u001b[39;00m\n\u001b[32m----> \u001b[39m\u001b[32m2\u001b[39m \u001b[43mdata\u001b[49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m \u001b[49m\u001b[43mweight\u001b[49m\n",
      "\u001b[31mValueError\u001b[39m: operands could not be broadcast together with shapes (8,2) (2,1) "
     ]
    }
   ],
   "source": [
    "# 树组间的运算必须要满足形状的要求（广播机制），即：维度相同 或 shape（其中相对应的一个地方为1），所以不能直接进行运算，会报错\n",
    "data * weight"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "99cc1dac-0cd7-4c46-b4e5-63d8f1673fcb",
   "metadata": {},
   "source": [
    "## 3.6 合并、分割"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "244c154b-cffb-4431-bc16-4a32725a0171",
   "metadata": {},
   "source": [
    "### 3.6.1 合并"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ed3cd2d8-3d67-44c3-8f06-23f0a5622596",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 2, 3, 4])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用np.hstack()方法水平拼接\n",
    "a = np.array([1,2,3])\n",
    "b = np.array([2,3,4])\n",
    "np.hstack((a,b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a55e1302-61fb-4675-b56f-0ac8d1394da2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [2, 3],\n",
       "       [3, 4]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 水平拼接\n",
    "a = np.array([[1], [2], [3]])\n",
    "b = np.array([[2], [3], [4]])\n",
    "np.hstack((a, b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "779f1460-3f76-4fd5-9e01-c35d7a050ab4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [2, 3, 4]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用np.vstack()方法垂直拼接\n",
    "a = np.array([1,2,3])\n",
    "b = np.array([2,3,4])\n",
    "np.vstack((a,b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "992a0400-91f8-4613-ac5f-6e644ee46df3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1],\n",
       "       [2],\n",
       "       [3],\n",
       "       [2],\n",
       "       [3],\n",
       "       [4]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 垂直拼接\n",
    "a = np.array([[1], [2], [3]])\n",
    "b = np.array([[2], [3], [4]])\n",
    "np.vstack((a, b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "e7390ce7-e902-49f0-b8bb-3299a0e75192",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4],\n",
       "       [5, 6]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用np.concatenate()方法拼接，第二个参数axis指定拼接的方向（即轴）\n",
    "a = np.array([[1, 2], [3, 4]])\n",
    "b = np.array([[5, 6]])\n",
    "\n",
    "# 垂直拼接\n",
    "np.concatenate((a, b), axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "9b777b53-1e6e-4b73-ab76-7521ef4ec918",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4],\n",
       "       [5, 6]])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vstack((a, b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "d77fe531-1de3-4f51-ad7f-a36e6fd7c441",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 5],\n",
       "       [3, 4, 6]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 水平拼接\n",
    "np.concatenate((a, b.T), axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "0307e30b-8984-4a43-806b-3a43c4f37434",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 5],\n",
       "       [3, 4, 6]])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hstack((a, b.T))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b68ec893-0485-475b-b213-7964f31e23d7",
   "metadata": {},
   "source": [
    "### 3.6.2 分割"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "9622bce9-1bc2-4483-9222-6a64a984cd81",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10., 11.])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.arange(12.0)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "950adca1-4a7d-4d20-a9df-927a1fc1787d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([0., 1., 2., 3.]), array([4., 5., 6., 7.]), array([ 8.,  9., 10., 11.])]"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将数组分割成三等份\n",
    "np.split(x, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "8232580a-26a4-49cc-b473-f175050b55b3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0., 1., 2., 3., 4., 5., 6., 7.])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.arange(8.0)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "7b3339b2-d49b-4395-a1e9-e2935e6b3f57",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([0., 1., 2.]),\n",
       " array([3., 4.]),\n",
       " array([5.]),\n",
       " array([6., 7.]),\n",
       " array([], dtype=float64)]"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 按指定索引（下标）进行分割\n",
    "np.split(x, [3, 5, 6, 10])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a1cc06a3-10a2-42e0-9fd9-2cea973982c0",
   "metadata": {},
   "source": [
    "## 3.7 IO操作与数据处理"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6df6b75c-e6cd-4049-83b8-ea57e3ea896a",
   "metadata": {},
   "source": [
    "### 3.7.1 Numpy读取文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "e55f45c8-35f2-4c5e-8601-6fa837ec4fb5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  nan,   nan,   nan,   nan],\n",
       "       [  1. , 123. ,   1.4,  23. ],\n",
       "       [  2. , 110. ,   nan,  18. ],\n",
       "       [  3. ,   nan,   2.1,  19. ]])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 读取数据\n",
    "test = np.genfromtxt(\"test.csv\", delimiter=\",\")\n",
    "test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "a191d916-fa96-4e04-b6c0-2aabfa0d0d88",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(nan)"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test[2][2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "7dd98d08-1466-4575-bb1a-c6c5fdc7fca0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 对缺失值进行插补处理的函数\n",
    "def fill_nan_by_column_mean(t):\n",
    "    # 按列遍历数组\n",
    "    for i in range(t.shape[1]):\n",
    "        # 计算当前列中nan的个数\n",
    "        nan_count = np.count_nonzero(t[:, i][t[:, i] != t[:, i]])\n",
    "        if nan_count > 0:\n",
    "            # 取出当前列\n",
    "            now_col = t[:, i]\n",
    "            # 求和\n",
    "            now_col_not_nan = now_col[np.isnan(now_col) == False].sum()\n",
    "            # 求该列已有值的平均数\n",
    "            now_col_mean = now_col_not_nan / (t.shape[0] - nan_count)\n",
    "            # 赋值给now_col\n",
    "            now_col[np.isnan(now_col)] = now_col_mean\n",
    "            # 赋值给t，即更新t的当前列\n",
    "            t[:, i] = now_col\n",
    "    return t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "ca5a4c59-ee2a-43db-976b-c6877b66f0b2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  2.  , 116.5 ,   1.75,  20.  ],\n",
       "       [  1.  , 123.  ,   1.4 ,  23.  ],\n",
       "       [  2.  , 110.  ,   1.75,  18.  ],\n",
       "       [  3.  , 116.5 ,   2.1 ,  19.  ]])"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 填补读取到的数据中的缺失值\n",
    "fill_nan_by_column_mean(test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e8e4b4a8-d0a5-4187-a87c-f6d72a5882e0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
